const path = require('path');

const csRenderBasePath = path.resolve('packages/core/src/index');
const csToolsBasePath = path.resolve('packages/tools/src/index');
const csAiBasePath = path.resolve('packages/ai/src/index');
const csLabelmapInterpolationBasePath = path.resolve(
  'packages/labelmap-interpolation/src/index'
);
const csPolymorphicSegmentationBasePath = path.resolve(
  'packages/polymorphic-segmentation/src/index'
);
const csAdapters = path.resolve('packages/adapters/src/index');
const csDICOMImageLoaderDistPath = path.resolve(
  'packages/dicomImageLoader/src/index'
);
const csNiftiPath = path.resolve('packages/nifti-volume-loader/src/index');

module.exports = function buildConfig(name, destPath, root, exampleBasePath) {
  return `
// THIS FILE IS AUTOGENERATED - DO NOT EDIT
const path = require('path')

const rules = require('./rules-examples.js');
const modules = [path.resolve('../node_modules/'), path.resolve('../../../node_modules/')];

const rspack = require('@rspack/core');

module.exports = {
  mode: 'development',
  devtool: 'inline-source-map',
  plugins: [
    new rspack.HtmlRspackPlugin({
      template: '${root.replace(/\\/g, '/')}/utils/ExampleRunner/template.html',
    }),
    new rspack.DefinePlugin({
      __BASE_PATH__: "''",
    }),
    new rspack.CopyRspackPlugin({
      patterns: [
        {
          from:
          '../../../node_modules/dicom-microscopy-viewer/dist/dynamic-import/',
          to: '${destPath.replace(/\\/g, '/') + '/dicom-microscopy-viewer'}',
          noErrorOnMissing: true,
        },
        {
          from:
            '../../../node_modules/onnxruntime-web/dist',
          to: '${destPath.replace(/\\/g, '/')}/ort',
        },
      ],
    }),
    new rspack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
    }),
  ],
  entry: path.join('${exampleBasePath.replace(/\\/g, '/')}'),
  output: {
    path: '${destPath.replace(/\\/g, '/')}',
    filename: '${name}.js',
  },
  module: {
    rules,
  },
  experiments: {
    asyncWebAssembly: true,
    nativeWatcher: true
  },
  externals: {
    "dicom-microscopy-viewer": {
      root: "window",
      commonjs: "dicomMicroscopyViewer",
    },
  },
  resolve: {
    alias: {
      '@cornerstonejs/core': '${csRenderBasePath.replace(/\\/g, '/')}',
      '@cornerstonejs/tools': '${csToolsBasePath.replace(/\\/g, '/')}',
      '@cornerstonejs/ai': '${csAiBasePath.replace(/\\/g, '/')}',
      '@cornerstonejs/polymorphic-segmentation': '${csPolymorphicSegmentationBasePath.replace(
        /\\/g,
        '/'
      )}',
      '@cornerstonejs/labelmap-interpolation': '${csLabelmapInterpolationBasePath.replace(
        /\\/g,
        '/'
      )}',
      '@cornerstonejs/nifti-volume-loader': '${csNiftiPath.replace(
        /\\/g,
        '/'
      )}',
      '@cornerstonejs/adapters': '${csAdapters.replace(/\\/g, '/')}',
      '@cornerstonejs/dicom-image-loader': '${csDICOMImageLoaderDistPath.replace(
        /\\/g,
        '/'
      )}',
    },
    modules,
    extensions: ['.ts', '.tsx', '.js', '.jsx'],
    fallback: {
      fs: false,
      path: require.resolve('path-browserify'),
      events: false,
      buffer: require.resolve('buffer'),
    },
  },
  devServer: {
    hot: true,
    open: false,
    port: ${process.env.CS3D_PORT || 3000},
    historyApiFallback: true,
    allowedHosts: [
      '127.0.0.1',
      'localhost',
    ],
  },
};
`;
};
